package com.aptemo.webetek.server.data;

import java.util.Date;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.aptemo.webetek.client.enums.EnumPaymentType;
import com.aptemo.webetek.shared.PaymentBean;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

/**
 * Stake is a User child class (owned 1:N bidirectional relationship)
 * 
 * Contains picks (unowned 1:N relationship)
 * 
 * @author coh
 * 
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Payment
{

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key				key;

	private String			sid;

	// owned 1:N bidirectional relationship
	// @Persistent
	// private WebetekUser player;
	@SuppressWarnings("unused")
	@Persistent
	@Extension(vendorName = "datanucleus", key = "gae.parent-pk", value = "true")
	private Key				parentKey;

	@Persistent
	private double			amount;

	@Persistent
	private Date			created;

	@Persistent
	private String			description;

	@Persistent
	private EnumPaymentType	type;

	/**
	 * usually Bet.id (for BET, EARNINGS)
	 */
	@Persistent
	private Key				reference;

	public Payment(double amount, String description, EnumPaymentType type)
	{
		this.amount = amount;
		this.description = description;
		this.type = type;
		this.created = new Date();
	}

	public String getDescription()
	{
		return description;
	}

	public void setDescription(String description)
	{
		this.description = description;
	}

	public EnumPaymentType getType()
	{
		return type;
	}

	public void setType(EnumPaymentType type)
	{
		this.type = type;
	}

	public WebetekUser getParent(PersistenceManager pm)
	{
		return pm.getObjectById(WebetekUser.class, key.getParent());
	}

	public void setAmount(double amount)
	{
		this.amount = amount;
	}

	public double getAmount()
	{
		return amount;
	}

	public String getSid()
	{
		if (sid == null)
		{
			this.sid = KeyFactory.keyToString(key);
		}
		return sid;
	}

	public Date getDate()
	{
		return created;
	}

	public PaymentBean toBean()
	{
		PaymentBean bean = new PaymentBean();
		bean.setId(getSid());
		bean.setAmount(getAmount());
		bean.setDescription(getDescription());
		bean.setDate(getDate());
		bean.setType(getType());

		return bean;
	}

	public Key getReference()
	{
		return reference;
	}

	public void setReference(Key reference)
	{
		this.reference = reference;
	}

}
